import { FakeApi, random } from '@wowon/utils';
import { getCurrentUser } from '../auth-center';

const initData = Array.from({ length: random(50, 200) }).map(() => {
  return {
    id: '',
    pid: '',
    title: '',
    url: '',
    remark: '',
  };
});

export const feedbackList = new FakeApi(initData, {
  queryType: {
    user_id: 'is',
    content: 'like',
  },
});

export default {
  'GET /mock/moniter/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.query(req.query);
    res.json(result);
  },
  'POST /mock/moniter/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.create(req.body);
    res.json(result);
  },
  'POST /mock/moniter/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.update(req.body);
    res.json(result);
  },
  'POST /mock/moniter/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.remove(req.body);
    res.json(result);
  },
  'POST /mock/moniter/list': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await feedbackList.list(req.query);
    res.json(result);
  },
};
